home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12228 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Is This Bad Coding Practice?
  5. Date: Fri, 29 Mar 96 21:31:55 GMT
  6. Organization: none
  7. Message-ID: <828135115snz@genesis.demon.co.uk>
  8. References: <4jgnt2$9d1@loki.tor.hookup.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4jgnt2$9d1@loki.tor.hookup.net>
  15.            Rajendra_Singh@msn.com "Rajendra Singh" writes:
  16.  
  17. >int main(int argc, char *argv[])
  18. >   {
  19. >   char string[128];
  20. >   char *func1(void);
  21. >
  22. >   strcpy(string, func1());
  23. >   return 0;
  24. >   }
  25. >
  26. >char *func1(void)
  27. >   {
  28. >   char test[100];
  29. >
  30. >   sprintf(test, "Test:  %d", 1);
  31. >   return test;
  32. >   }
  33. >
  34. >... since I am using the value returned from func1() immediately (in
  35. >main()), is this reliable?
  36.  
  37. No, as soon as func1 returns test doesn't exist any more so you cannot
  38. safely access it (doing so results in undefined behaviour). Don't
  39. do it, even if it seems to work on your system - it will bite you, sooner
  40. rather than later if you're lucky.
  41.  
  42. -- 
  43. -----------------------------------------
  44. Lawrence Kirby | fred@genesis.demon.co.uk
  45. Wilts, England | 70734.126@compuserve.com
  46. -----------------------------------------
  47.